package com.net.xpay.admin.controller.admin;

import com.alipay.api.AlipayApiException;
import com.alipay.api.response.AlipayFundBatchDetailQueryResponse;
import com.net.common.exception.BusinessException;
import com.net.common.model.ResultData;
import com.net.common.model.ResultPagedList;
import com.net.common.util.SerializationUtil;
import com.net.xpay.common.domain.UserShard;
import com.net.xpay.common.domain.sync.CommonPayTransBatch;
import com.net.xpay.common.manager.UserShardManager;
import com.net.xpay.common.manager.sync.CommonPayTransBatchManager;
import com.net.xpay.core.constant.PoseidonErrorCode;
import com.net.xpay.core.domain.PayTransSummary;
import com.net.xpay.core.manager.PayTransSummaryManager;
import com.net.xpay.core.model.PayTransBatchSearchModel;
import com.net.xpay.core.service.AlipayFundBatchService;
import com.net.shard.ShardUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *

 */
@Slf4j
@RestController
@RequestMapping("/admin/pay-trans-batch")
@Api(tags = "运营/商户打款批次", description = "运营 商户打款批次 net")
public class AdminPayTransBatchController {

    @Autowired
    private CommonPayTransBatchManager commonPayTransBatchManager;

    @Autowired
    private AlipayFundBatchService alipayFundBatchService;

    @Autowired
    private UserShardManager userShardManager;

    @Autowired
    private PayTransSummaryManager payTransSummaryManager;


    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'pay-trans-batch::search')")
    public ResultPagedList<CommonPayTransBatch> searchPayTransBatch(@RequestBody PayTransBatchSearchModel searchModel) {
        List<CommonPayTransBatch> records = commonPayTransBatchManager.search(searchModel);
        long count = commonPayTransBatchManager.searchCount(searchModel);

        return new ResultPagedList<>(records, count, searchModel);
    }

    @ApiOperation("搜索单个用户")
    @GetMapping
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'pay-trans-batch::get')")
    public ResultData<CommonPayTransBatch> getPayTransBatch( Long id) {
        CommonPayTransBatch batchManagerById = commonPayTransBatchManager.getById(id);

        return new ResultData<>(batchManagerById);
    }

    @ApiOperation("支付宝接口查询")
    @PostMapping("/fetch-from-alipay")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN')")
    public ResultData<String> fetchPayTransBatchFromAlipay(@RequestBody PayTransBatchSearchModel searchModel) {
        UserShard userShard = userShardManager.getById(searchModel.getUserId());
        try {
            ShardUtil.setShardContext(userShard.getShardId());
            List<CommonPayTransBatch> commonPayTransBatches = commonPayTransBatchManager.search(searchModel);
            CommonPayTransBatch batch = commonPayTransBatches.get(0);
            PayTransSummary summary = payTransSummaryManager.findById(searchModel.getUserId(), batch.getLastSummaryId());
            AlipayFundBatchDetailQueryResponse resp = alipayFundBatchService.allDetailQuery(summary.getAlipayUserId(), summary.getOutBatchNo());
            return new ResultData<>(SerializationUtil.obj2String(resp));
        } catch (AlipayApiException e) {
            log.error("fetchPayTransBatchFromAlipay error", e);
            throw new BusinessException(PoseidonErrorCode.PARAMETER_ILLEGAL, "系统错误");
        } finally {
            ShardUtil.cleanShardContext();
        }

    }
}
